package tetris;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Tetris extends Frame implements ActionListener{
    private Button start = new Button("Start");
    private Button quit = new Button("Quit");
    private Button highscore = new Button("High score");
    private Button pause = new Button("Pause");
    private Button restart = new Button("Restart!");
    private Button qback = new Button("Quit&back!");
    private Label score = new Label("   SCORE");
    private Label line = new Label("     LINE");
    private Label level = new Label("     LEVEL");
    private Label sscore = new Label("    "+0);
    private Label lline = new Label("    "+0);
    private Label llevel = new Label("    "+1);
    private Keylis keylistener = new Keylis();
    Toolkit kit=Toolkit.getDefaultToolkit();
    Image headpic=kit.getImage("pic.jpg");
    int onlyonce=0,delay;
    int p,q,tempx,tempy,linenum,scorenum,levelnum,templine,gameovertime;
    int[][] xy = new int[4][2];
    int[][] nextxy = new int[4][2];
    int a,b,c,d,n;
    boolean havesquare=false,playable=false,pauseable;
    boolean[] tempbool = new boolean[10];
    Gamewin gamewin = new Gamewin();
    Nextwin nextwin = new Nextwin();
    boolean[][] winmain = new boolean[10][20];
    boolean[][] next = new boolean[4][4];
    Timer timer=new Timer();
    Createsquare cs = new Createsquare();
    float[] nowcolor = new float[4];        //当前块色
    float[] nextcolor = new float[4];       //下一块色
    float[][][] cc = new float[10][20][4];  //颜色矩阵
    float[] bgcolor = {1f,1f,1f,1f};
    
    public class WindowCloser extends WindowAdapter{
    public void windowClosing(WindowEvent we){
            System.exit(0);
            }
        }

    public int gethighest(int n1,int n2,int n3,int n4){
        int temp;
        temp=n1;
        if (temp<n2) temp=n2;
        if (temp<n3) temp=n3;
        if (temp<n4) temp=n4;
        return(temp);
    }
    public int getsmallest(int n1,int n2,int n3,int n4){
        int temp;
        temp=n1;
        if (temp>n2) temp=n2;
        if (temp>n3) temp=n3;
        if (temp>n4) temp=n4;
        return(temp);
    }
    public void buttonset(){
        start.setBounds(30, 170, 70, 20 );
        highscore.setBounds(30, 230, 70, 20 );
        quit.setBounds(30, 290, 70, 20 );
        start.setBackground(Color.orange);
        highscore.setBackground(Color.orange);
        quit.setBackground(Color.orange);
    }
    public void buttonsee(boolean f){
        quit.setVisible(f);
        start.setVisible(f);
        highscore.setVisible(f);
    }
    public void labelset(){
        score.setBounds(35, 120, 70, 20);
        sscore.setBounds(35, 150, 70, 20);
        line.setBounds(35, 190, 70, 20);
        lline.setBounds(35, 220, 70, 20);
        level.setBounds(35, 260, 70, 20);
        llevel.setBounds(35, 290, 70, 20);
        pause.setBounds(20, 330, 100, 20);
        restart.setBounds(20, 360, 100, 20);
        qback.setBounds(20, 390, 100, 20);
        
        score.setBackground(Color.orange);
        line.setBackground(Color.orange);
        level.setBackground(Color.orange);
        sscore.setBackground(Color.green);
        lline.setBackground(Color.green);
        llevel.setBackground(Color.green);
        pause.setBackground(Color.ORANGE);
        restart.setBackground(Color.ORANGE);
        qback.setBackground(Color.ORANGE);
    }
    public void labelsee(boolean f){
        score.setVisible(f);
        line.setVisible(f);
        level.setVisible(f);
        sscore.setVisible(f);
        lline.setVisible(f);
        llevel.setVisible(f);
        pause.setVisible(f);
        restart.setVisible(f);
        qback.setVisible(f);
    }
    public Tetris(){
        super("Tetris.");
        setBackground(Color.gray);
        setLayout(null);
        setIconImage(headpic);
        
        buttonset();
        add(start);
        add(highscore);
        add(quit);
        start.addActionListener(this);
        highscore.addActionListener(this);
        quit.addActionListener(this);
        pause.addActionListener(this);
        restart.addActionListener(this);
        qback.addActionListener(this);
        
        add(score);add(level);add(line);
        add(sscore);add(llevel);add(lline);add(restart);add(qback);add(pause);
        labelset();
        labelsee(false);

        add(nextwin);
        nextwin.setBounds(30, 35, 80, 80);

        for (int u=0;u<=19;u++){
                add(gamewin.gameline[u]);
                gamewin.gameline[u].setBackground(Color.darkGray);
                gamewin.gameline[u].setBounds(150, 30+20*u,200, 20);
                gamewin.win=this.winmain;
                gamewin.gameline[u].setVisible(true);
        }

        addWindowListener(new WindowCloser());
        addKeyListener(this.keylistener);
        
        setSize(350,435);
        setResizable(false);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e){
        if (e.getSource()==quit) System.exit(0);
        else if (e.getSource()==start)  gamestart();
        else if (e.getSource()==highscore) showscore();
        else if (e.getSource()==restart) {
            while(!this.requestFocusInWindow()){
                this.requestFocusInWindow();
            }
            gamestart();
        }
        else if (e.getSource()==qback) {
            playable=false;
            labelsee(false);
            buttonsee(true);
            gamewin.playover=true;
            gamewin.gameover();
            gameovertime=1;
        }
        else if (e.getSource()==pause){
            if (pause.getLabel()=="Pause"){
                pauseable=true;
                pause.setLabel("Start");
            }
            else if (pause.getLabel()=="Start"){
                pauseable=false;
                pause.setLabel("Pause");
            }
        }
    }
    public void create(){
        getscore();
        Square now = new Square();
        xy = nextxy;nowcolor = nextcolor;
        nextxy = now.sxy; nextcolor = now.scolor;
        
        tempbool[0]=(winmain[xy[0][0]][xy[0][1]]==false);
        tempbool[1]=(winmain[xy[1][0]][xy[1][1]]==false);
        tempbool[2]=(winmain[xy[2][0]][xy[2][1]]==false);
        tempbool[3]=(winmain[xy[3][0]][xy[3][1]]==false);
     
        winmain[xy[0][0]][xy[0][1]]=false;  winmain[xy[1][0]][xy[1][1]]=false;
        winmain[xy[2][0]][xy[2][1]]=false;  winmain[xy[3][0]][xy[3][1]]=false;
        cc[xy[0][0]][xy[0][1]]=nowcolor;  cc[xy[1][0]][xy[1][1]]=nowcolor;
        cc[xy[2][0]][xy[2][1]]=nowcolor;  cc[xy[3][0]][xy[3][1]]=nowcolor;
        gamewin.win=winmain;    gamewin.gamergb=cc;
        havesquare=true;
      
        if(tempbool[0]||tempbool[1]||tempbool[2]||tempbool[3]){
            gamewin.playover=true;
            playable=false;
            labelsee(false);
            buttonsee(true);
            p=gethighest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
            q=getsmallest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
            gamewin.drawline(p, q);
            return;
        }
        p=gethighest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
        q=getsmallest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
        gamewin.drawline(p,q);

        for (int i=0;i<=3;i++)
                for (int j=0;j<=3;j++){
                    nextwin.win[i][j]=true;
//                    nextwin.c[i][j]=bgcolor;
                }
        nextwin.win[nextxy[0][0]-3][nextxy[0][1]]=false;        nextwin.win[nextxy[1][0]-3][nextxy[1][1]]=false;
        nextwin.win[nextxy[2][0]-3][nextxy[2][1]]=false;        nextwin.win[nextxy[3][0]-3][nextxy[3][1]]=false;
        nextwin.c[xy[0][0]-3][xy[0][1]]=nextcolor;  nextwin.c[xy[1][0]-3][xy[1][1]]=nextcolor;
        nextwin.c[xy[2][0]-3][xy[2][1]]=nextcolor;  nextwin.c[xy[3][0]-3][xy[3][1]]=nextcolor;
        nextwin.repaint();
    }
    public boolean leftable(){
        tempbool[4]=(xy[0][0]>0)&&(xy[1][0]>0)&&(xy[2][0]>0)&&(xy[3][0]>0);
        if (!tempbool[4]) return(false);
        winmain[xy[0][0]][xy[0][1]]=true;
        winmain[xy[1][0]][xy[1][1]]=true;
        winmain[xy[2][0]][xy[2][1]]=true;
        winmain[xy[3][0]][xy[3][1]]=true;
        tempbool[0]=(winmain[xy[0][0]-1][xy[0][1]]==true);
        tempbool[1]=(winmain[xy[1][0]-1][xy[1][1]]==true);
        tempbool[2]=(winmain[xy[2][0]-1][xy[2][1]]==true);
        tempbool[3]=(winmain[xy[3][0]-1][xy[3][1]]==true);
        winmain[xy[0][0]][xy[0][1]]=false;
        winmain[xy[1][0]][xy[1][1]]=false;
        winmain[xy[2][0]][xy[2][1]]=false;
        winmain[xy[3][0]][xy[3][1]]=false;
        return(tempbool[0]&&tempbool[1]&&tempbool[2]&&tempbool[3]);
    }
    public boolean rightable(){
        tempbool[4]=(xy[0][0]<9)&&(xy[1][0]<9)&&(xy[2][0]<9)&&(xy[3][0]<9);
        if (!tempbool[4]) return(false);
        winmain[xy[0][0]][xy[0][1]]=true;
        winmain[xy[1][0]][xy[1][1]]=true;
        winmain[xy[2][0]][xy[2][1]]=true;
        winmain[xy[3][0]][xy[3][1]]=true;
        tempbool[0]=(winmain[xy[0][0]+1][xy[0][1]]==true);
        tempbool[1]=(winmain[xy[1][0]+1][xy[1][1]]==true);
        tempbool[2]=(winmain[xy[2][0]+1][xy[2][1]]==true);
        tempbool[3]=(winmain[xy[3][0]+1][xy[3][1]]==true);
        winmain[xy[0][0]][xy[0][1]]=false;
        winmain[xy[1][0]][xy[1][1]]=false;
        winmain[xy[2][0]][xy[2][1]]=false;
        winmain[xy[3][0]][xy[3][1]]=false;
        return(tempbool[0]&&tempbool[1]&&tempbool[2]&&tempbool[3]);
    }
    public boolean downable(){
        this.requestFocusInWindow();
        tempbool[4]=(xy[0][1]<19)&&(xy[1][1]<19)&&(xy[2][1]<19)&&(xy[3][1]<19);
        if (!tempbool[4]) return(false);
        winmain[xy[0][0]][xy[0][1]]=true;
        winmain[xy[1][0]][xy[1][1]]=true;
        winmain[xy[2][0]][xy[2][1]]=true;
        winmain[xy[3][0]][xy[3][1]]=true;
        tempbool[0]=(winmain[xy[0][0]][xy[0][1]+1]==true);
        tempbool[1]=(winmain[xy[1][0]][xy[1][1]+1]==true);
        tempbool[2]=(winmain[xy[2][0]][xy[2][1]+1]==true);
        tempbool[3]=(winmain[xy[3][0]][xy[3][1]+1]==true);
        winmain[xy[0][0]][xy[0][1]]=false;
        winmain[xy[1][0]][xy[1][1]]=false;
        winmain[xy[2][0]][xy[2][1]]=false;
        winmain[xy[3][0]][xy[3][1]]=false;
        return((tempbool[0]&&tempbool[1])&&(tempbool[2]&&tempbool[3]));
    }
    public boolean upable(){
        if ((xy[0][0]==xy[1][0] && xy[0][1]+1==xy[1][1])&&(xy[2][0]==xy[3][0] && xy[2][1]+1==xy[3][1]))
            return(false);
        winmain[xy[0][0]][xy[0][1]]=true;
        winmain[xy[1][0]][xy[1][1]]=true;
        winmain[xy[2][0]][xy[2][1]]=true;
        winmain[xy[3][0]][xy[3][1]]=true;
        a=xy[0][0];b=xy[0][1];
        for (int s=1;s<=3;s++){
            c=xy[s][0];d=xy[s][1];
            if (c==a) {                 //在同一列
                c=a+b-d;d=b;
                if ((-1<c)&&(c<10)&&(-1<d)&&(d<20))
                tempbool[s]=(winmain[c][d]==true);
                else return(false);
            }
            else if (b==d){         //在同一行
                d=b+c-a;c=a;
                if ((-1<c)&&(c<10)&&(-1<d)&&(d<20))
                tempbool[s]=(winmain[c][d]==true);
                else return(false);
            }
            else {                  //不再同行同列
                if (c>a && d>b) c=c-2;
                else if(c<a && d>b) d=d-2;
                else if(c<a && d <b)c=c+2;
                else if(c>a && d<b) d=d+2;
                if ((-1<c)&&(c<10)&&(-1<d)&&(d<20))
                tempbool[s]=(winmain[c][d]==true);
                else return(false);
            }
        }
        winmain[xy[0][0]][xy[0][1]]=false;
        winmain[xy[1][0]][xy[1][1]]=false;
        winmain[xy[2][0]][xy[2][1]]=false;
        winmain[xy[3][0]][xy[3][1]]=false;
        return(tempbool[1]&&tempbool[2]&&tempbool[3]);
    }
    public class Keylis implements KeyListener{
        public void keyReleased(KeyEvent ke){
        }
        public void keyTyped(KeyEvent ke){

        }
        public void keyPressed(KeyEvent ke){
            if (!playable||!havesquare||pauseable) return;
            if (ke.getKeyCode()==37){                  //left
                if (leftable()){
                    winmain[xy[0][0]][xy[0][1]]=true;                    winmain[xy[1][0]][xy[1][1]]=true;
                    winmain[xy[2][0]][xy[2][1]]=true;                    winmain[xy[3][0]][xy[3][1]]=true;
//                    cc[xy[0][0]][xy[0][1]]=bgcolor;  cc[xy[1][0]][xy[1][1]]=bgcolor;
//                    cc[xy[2][0]][xy[2][1]]=bgcolor;  cc[xy[3][0]][xy[3][1]]=bgcolor;
                    
                    winmain[xy[0][0]-1][xy[0][1]]=false;                 winmain[xy[1][0]-1][xy[1][1]]=false;
                    winmain[xy[2][0]-1][xy[2][1]]=false;                 winmain[xy[3][0]-1][xy[3][1]]=false;
                    cc[xy[0][0]-1][xy[0][1]]=nowcolor;  cc[xy[1][0]-1][xy[1][1]]=nowcolor;
                    cc[xy[2][0]-1][xy[2][1]]=nowcolor;  cc[xy[3][0]-1][xy[3][1]]=nowcolor;
                    xy[0][0]--;xy[1][0]--;xy[2][0]--;xy[3][0]--;
                    gamewin.win=winmain; gamewin.gamergb=cc;
                    p=gethighest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    q=getsmallest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    gamewin.drawline(p, q);
                }
            }
            else if(ke.getKeyCode()==38){               //up
                if (upable()){
                    a=xy[0][0];b=xy[0][1];
                    winmain[xy[1][0]][xy[1][1]]=true;
                    winmain[xy[2][0]][xy[2][1]]=true;
                    winmain[xy[3][0]][xy[3][1]]=true;
                    cc[xy[0][0]][xy[0][1]]=bgcolor;  cc[xy[1][0]][xy[1][1]]=bgcolor;
                    cc[xy[2][0]][xy[2][1]]=bgcolor;  cc[xy[3][0]][xy[3][1]]=bgcolor;
                    p=gethighest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    q=getsmallest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    gamewin.drawline(p, q);
                    a=xy[0][0];b=xy[0][1];
                    for (int s=1;s<=3;s++){
                        c=xy[s][0];d=xy[s][1];
                        if (c==a) {                 //在同一列
                            c=a+b-d;d=b;
                            xy[s][0]=c;xy[s][1]=d;
                        }
                        else if (b==d){         //在同一行
                            d=b+c-a;c=a;
                            xy[s][0]=c;xy[s][1]=d;
                        }
                        else {
                            if (c>a && d>b) c=c-2;
                            else if(c<a && d>b) d=d-2;
                            else if(c<a && d <b)c=c+2;
                            else if(c>a && d<b) d=d+2;
                        }
                        cc[xy[0][0]][xy[0][1]]=nowcolor;
                        winmain[c][d]=false;
                        cc[c][d]=nowcolor;
                        xy[s][0]=c;xy[s][1]=d;
                    }
                    gamewin.win=winmain;gamewin.gamergb=cc;
                    p=gethighest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    q=getsmallest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    gamewin.drawline(p, q);
                }
            }
            else if(ke.getKeyCode()==39){               //right
                if(rightable()){
                    winmain[xy[0][0]][xy[0][1]]=true;                    winmain[xy[1][0]][xy[1][1]]=true;
                    winmain[xy[2][0]][xy[2][1]]=true;                    winmain[xy[3][0]][xy[3][1]]=true;
//                    cc[xy[0][0]][xy[0][1]]=bgcolor;  cc[xy[1][0]][xy[1][1]]=bgcolor;
//                    cc[xy[2][0]][xy[2][1]]=bgcolor;  cc[xy[3][0]][xy[3][1]]=bgcolor;
                    
                    winmain[xy[0][0]+1][xy[0][1]]=false;                   winmain[xy[1][0]+1][xy[1][1]]=false;
                    winmain[xy[2][0]+1][xy[2][1]]=false;                   winmain[xy[3][0]+1][xy[3][1]]=false;
                    cc[xy[0][0]+1][xy[0][1]]=nowcolor;  cc[xy[1][0]+1][xy[1][1]]=nowcolor;
                    cc[xy[2][0]+1][xy[2][1]]=nowcolor;  cc[xy[3][0]+1][xy[3][1]]=nowcolor;

                    xy[0][0]++;xy[1][0]++;xy[2][0]++;xy[3][0]++;
                    gamewin.win=winmain;gamewin.gamergb=cc;
                    p=gethighest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    q=getsmallest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    gamewin.drawline(p, q);
                }
            }
            else if(ke.getKeyCode()==40){               //down
                if(downable()){
                    winmain[xy[0][0]][xy[0][1]]=true;                    winmain[xy[1][0]][xy[1][1]]=true;
                    winmain[xy[2][0]][xy[2][1]]=true;                    winmain[xy[3][0]][xy[3][1]]=true;
//                    cc[xy[0][0]][xy[0][1]]=bgcolor;  cc[xy[1][0]][xy[1][1]]=bgcolor;
//                    cc[xy[2][0]][xy[2][1]]=bgcolor;  cc[xy[3][0]][xy[3][1]]=bgcolor;
                    p=gethighest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    q=getsmallest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    gamewin.drawline(p, q);
                    
                    winmain[xy[0][0]][xy[0][1]+1]=false;                    winmain[xy[1][0]][xy[1][1]+1]=false;
                    winmain[xy[2][0]][xy[2][1]+1]=false;                    winmain[xy[3][0]][xy[3][1]+1]=false;
                    cc[xy[0][0]][xy[0][1]+1]=nowcolor;  cc[xy[1][0]][xy[1][1]+1]=nowcolor;
                    cc[xy[2][0]][xy[2][1]+1]=nowcolor;  cc[xy[3][0]][xy[3][1]+1]=nowcolor;
                    
                    xy[0][1]++;xy[1][1]++;xy[2][1]++;xy[3][1]++;
                    gamewin.win=winmain;gamewin.gamergb=cc;
                    p=gethighest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    q=getsmallest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    gamewin.drawline(p, q);
                }
            }
            else if(ke.getKeyCode()==32){               //spacebar
                while(downable()){
                    winmain[xy[0][0]][xy[0][1]]=true;                    winmain[xy[1][0]][xy[1][1]]=true;
                    winmain[xy[2][0]][xy[2][1]]=true;                    winmain[xy[3][0]][xy[3][1]]=true;
//                    cc[xy[0][0]][xy[0][1]]=bgcolor;  cc[xy[1][0]][xy[1][1]]=bgcolor;
//                    cc[xy[2][0]][xy[2][1]]=bgcolor;  cc[xy[3][0]][xy[3][1]]=bgcolor;
                    p=gethighest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    q=getsmallest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    gamewin.drawline(p, q);

                    winmain[xy[0][0]][xy[0][1]+1]=false;                    winmain[xy[1][0]][xy[1][1]+1]=false;
                    winmain[xy[2][0]][xy[2][1]+1]=false;                    winmain[xy[3][0]][xy[3][1]+1]=false;
                    cc[xy[0][0]][xy[0][1]+1]=nowcolor;  cc[xy[1][0]][xy[1][1]+1]=nowcolor;
                    cc[xy[2][0]][xy[2][1]+1]=nowcolor;  cc[xy[3][0]][xy[3][1]+1]=nowcolor;

                    xy[0][1]++;xy[1][1]++;xy[2][1]++;xy[3][1]++;                   
                }
                gamewin.win=winmain;gamewin.gamergb=cc;
                p=gethighest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                q=getsmallest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    gamewin.drawline(p, q);
                havesquare=false;
                if (xy[0][1]==0||xy[1][1]==0||xy[2][1]==0||xy[3][1]==0){
                    playable=false;
                    gamewin.gameover();
                    gameovertime=1;
                }
            }
        }
    }
    public void showscore(){
        
    }
    public void getscore(){
        templine=0;
        for (int s=19;s>=0;s--){
            for (int t=0;t<=9;t++){
                if (winmain[t][s]==true) break;
                if (t==9){
                    templine++;
                    for (int i=s-1,v;i>=0;i--){
                        for (int j=0;j<=9;j++){
                            v=i+1;
                            winmain[j][v]=winmain[j][i];cc[j][v]=cc[j][i];
                        }
                    }
                    gamewin.win=winmain;gamewin.gamergb=cc;
                    gamewin.drawline(19, 0);
                    s++;
                }
            }
        }
        switch(templine){
            case 0: break;
            case 1: scorenum+=100;break;
            case 2: scorenum+=300;break;
            case 3: scorenum+=500;break;
            case 4: scorenum+=800;break;
            default:break;
        }
        sscore.setText("    "+scorenum);
        linenum=linenum+templine;
        lline.setText("    "+linenum);
        levelnum=Math.round(scorenum/1000);
        if (levelnum>=9) 
            levelnum=9;
        llevel.setText("    "+levelnum);
        delay=1000-levelnum*100;
    }

    public class Createsquare extends TimerTask{
        public void run(){
            if (pauseable) return;
            if (!playable ){
                if (gameovertime==0){
                    gamewin.gameover();
                    gameovertime=1;
                }
                return;
            }
            if (!havesquare) {
                create();return;
            }
            if (++n >= delay/50){
                n=0;
                if(downable()){
                    winmain[xy[0][0]][xy[0][1]]=true;                    winmain[xy[1][0]][xy[1][1]]=true;
                    winmain[xy[2][0]][xy[2][1]]=true;                    winmain[xy[3][0]][xy[3][1]]=true;
//                    cc[xy[0][0]][xy[0][1]]=bgcolor;  cc[xy[1][0]][xy[1][1]]=bgcolor;
//                    cc[xy[2][0]][xy[2][1]]=bgcolor;  cc[xy[3][0]][xy[3][1]]=bgcolor;
                    p=gethighest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    q=getsmallest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    gamewin.drawline(p, q);

                    winmain[xy[0][0]][xy[0][1]+1]=false;                    winmain[xy[1][0]][xy[1][1]+1]=false;
                    winmain[xy[2][0]][xy[2][1]+1]=false;                    winmain[xy[3][0]][xy[3][1]+1]=false;
                    cc[xy[0][0]][xy[0][1]+1]=nowcolor;  cc[xy[1][0]][xy[1][1]+1]=nowcolor;
                    cc[xy[2][0]][xy[2][1]+1]=nowcolor;  cc[xy[3][0]][xy[3][1]+1]=nowcolor;

                    xy[0][1]++;xy[1][1]++;xy[2][1]++;xy[3][1]++;
                    gamewin.win=winmain;gamewin.gamergb=cc;
                    p=gethighest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    q=getsmallest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
                    gamewin.drawline(p, q);
                }
                else {
                    havesquare=false;
                    if (xy[0][1]==0||xy[1][1]==0||xy[2][1]==0||xy[3][1]==0){
                        playable=false;
                        gamewin.gameover();
                        gameovertime=1;
                    }
                }
            }
        }
    }
    public void gamestart(){
        scorenum=0;levelnum=0;templine=0;gameovertime=0;
        delay=1000-levelnum*100;n=0;
        buttonsee(false);
        labelsee(true);
        playable=true;   
        gamewin.playover=false;pauseable=false;

        for (int i=0;i<=9;i++)
            for (int j=0;j<=19;j++){
                winmain[i][j]=true;
                cc[i][j]=bgcolor;
            }
        gamewin.win=winmain;
        p=gethighest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
        q=getsmallest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
        gamewin.drawline(p, q);
        for (int s=0;s<=3;s++)
                for (int t=0;t<=3;t++){
                    nextwin.win[s][t]=true;
                    nextwin.c[s][t]=bgcolor;
                }
        nextwin.repaint();

        havesquare=true;
        Square now1 = new Square();
        xy = now1.sxy; nowcolor=now1.scolor;
        Square now2 = new Square();
        nextxy = now2.sxy;  nextcolor = now2.scolor;
        winmain[xy[0][0]][xy[0][1]]=false;        winmain[xy[1][0]][xy[1][1]]=false;
        winmain[xy[2][0]][xy[2][1]]=false;        winmain[xy[3][0]][xy[3][1]]=false;
        cc[xy[0][0]][xy[0][1]]=nowcolor;  cc[xy[1][0]][xy[1][1]]=nowcolor;
        cc[xy[2][0]][xy[2][1]]=nowcolor;  cc[xy[3][0]][xy[3][1]]=nowcolor;
        gamewin.win=winmain;gamewin.gamergb=cc;
        p=gethighest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
        q=getsmallest(xy[0][1],xy[1][1],xy[2][1],xy[3][1]);
        gamewin.drawline(p, q);
        nextwin.win[nextxy[0][0]-3][nextxy[0][1]]=false;        nextwin.win[nextxy[1][0]-3][nextxy[1][1]]=false;
        nextwin.win[nextxy[2][0]-3][nextxy[2][1]]=false;        nextwin.win[nextxy[3][0]-3][nextxy[3][1]]=false;
        nextwin.c[xy[0][0]-3][xy[0][1]]=nextcolor;  nextwin.c[xy[1][0]-3][xy[1][1]]=nextcolor;
        nextwin.c[xy[2][0]-3][xy[2][1]]=nextcolor;  nextwin.c[xy[3][0]-3][xy[3][1]]=nextcolor;
        nextwin.repaint();

        gamewin.drawline(19, 0);
        if(onlyonce==0){
            for (int u=0;u<=19;u++){
                gamewin.gameline[u].setBackground(Color.BLACK);
            }
            timer.schedule(cs, 0, 50);
            onlyonce=1;
        }
    }

    public static void main(String[] args) {
        new Tetris();
    }

}